package com.homer.android.activity.scene;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.homer.android.R;
import com.homer.android.activity.device.DeviceActivityAbs;
import com.homer.android.activity.main.EspActivityAbs;
import com.homer.android.activity.view.DeviceAdapter;
import com.homer.android.activity.view.DeviceAdapter.OnEditCheckedChangeListener;
import com.homer.android.activity.view.DeviceSceneAdapter;
import com.homer.android.activity.view.TouchPointMoveLayout;
import com.homer.android.activity.view.TouchPointMoveLayout.IntersectsView;
import com.homer.android.activity.view.TouchPointMoveLayout.OnTouchMoveListener;
import com.homer.android.sdk.devcie.IEspDevice;
import com.homer.android.sdk.devcie.array.IEspDeviceArray;
import com.homer.android.sdk.devcie.bulider.BEspDevice;
import com.homer.android.sdk.group.IEspGroup;
import com.homer.android.sdk.object.db.EspGroupDBManager;
import com.homer.android.sdk.type.devcie.IEspDeviceState;
import com.homer.android.sdk.type.user.builder.BEspUser;
import com.homer.android.sdk.user.IEspUser;
import com.homer.android.sdk.util.EspStrings;
import com.homer.iot.esptouch.model.group.EspGroupHandler;
import com.homer.iot.esptouch.udp.EspDeviceType;

public class EspSceneActivity extends EspActivityAbs implements OnClickListener, OnItemLongClickListener, OnItemClickListener, OnMenuItemClickListener, OnEditCheckedChangeListener {
    private IEspUser mUser;

    private List<IEspDevice> mUserDeviceList;

    private ListView mDeviceLV;

    private List<IEspDevice> mDeviceList;

    private DeviceAdapter mDeviceAdapter;

    private View mSceneContent;

    private ImageView mSceneAddIV;

    private ListView mSceneLV;

    private List<IEspGroup> mSceneList;

    private SceneAdapter mSceneAdapter;

    private TouchPointMoveLayout mMoveLayout;

    private static final int POPMENU_ID_SYNC_LOCAL = 0x10;

    private static final int POPMENU_ID_EDIT = 0x11;

    private static final int POPMENU_ID_REMOVE = 0x12;

    private static final int POPMENU_ID_CONTROL = 0x13;

    /************************* 手动添加的场景控制菜单项 *************************************/
    private static final int POPMENU_ID_SCENE_CONTROL = 0x14;

    private static final int POPMENU_ID_RENAME = 0x20;

    private static final int POPMENU_ID_DELETE = 0x21;

    private MenuItem mEditMenuItem;

    private MenuItem mRemoveMenuItem;

    private MenuItem mControlMenuItem;

    /************************* 手动添加的场景控制菜单项 *************************************/
    private MenuItem mSceneControlMenuItem;

    private Button scene_excute;

    private Button scene_save;

    private EspGroupHandler mEspGroupHandler;

    private EspGroupDBManager mEspGroupDBManager;

    public static IEspGroup mSelectedGroup;

    private String mTitle;

    private LocalBroadcastManager mBroadcastManager;
    
   

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.esp_scene_activity);

        mUser = BEspUser.getBuilder().getInstance();
        mAllDeviceList = new Vector<IEspDevice>();
        mUserDeviceList = mUser.getAllDeviceList();
        updateDeviceList();
        mTitle = getString(R.string.esp_scene);
        setTitle(mTitle);
        setTitleRightIcon(R.drawable.esp_icon_menu_moreoverflow);

        mMoveLayout = (TouchPointMoveLayout) findViewById(R.id.container);
        mMoveLayout.setOnIntersectsChangeListener(mIntersectsListener);

        scene_excute = (Button) findViewById(R.id.scene_excute);
        scene_save = (Button) findViewById(R.id.scene_save);

        scene_excute.setVisibility(View.GONE);
        scene_save.setVisibility(View.GONE);      
        scene_excute.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

//                // DeviceSceneAdapter.array
//                for (int i=0;i< DeviceSceneAdapter.array.length;i++) {
//                    IEspDevice espDevice = mDeviceList.get(i);
//                   final  IEspDevice device = espDevice.cloneDevice();
//                    final EspStatusPlug status = new EspStatusPlug();
//                    if (DeviceSceneAdapter.array[i] == 0) {
//                        Toast.makeText(EspSceneActivity.this, "开关:"+espDevice.getName()+":关闭",  Toast.LENGTH_SHORT).show();
//                        boolean isOn = false;                       
//                        status.setIsOn(isOn);                       
//                      
//                    } else if (DeviceSceneAdapter.array[i] == 1) {
//                        boolean isOn = true;                      
//                        status.setIsOn(isOn);                     
//                        Toast.makeText(EspSceneActivity.this, "开关:"+espDevice.getName()+":打开", Toast.LENGTH_SHORT).show();
//                    }
//                    
//                    
//                    IEspDeviceState state = device.getDeviceState();
//                    if (state.isStateInternet() || state.isStateLocal()) {
//                        new Thread(new Runnable() {
//                            
//                            @Override
//                            public void run() {
//                                mUser.doActionPostDeviceStatus(device, status);
//                                
//                            }
//                        }).start();
//                      
//                    }
//                    try {
//                        Thread.sleep(100);
//                    } catch (InterruptedException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    }
//                }

            }
        });
        
        scene_save.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
				//实例化SharedPreferences对象（第一步） 
				SharedPreferences mySharedPreferences= getSharedPreferences("scene",Activity.MODE_PRIVATE); 
				//实例化SharedPreferences.Editor对象（第二步） 
				SharedPreferences.Editor editor = mySharedPreferences.edit(); 
				//用putString的方法保存数据 
				//editor.putString("name", DeviceSceneAdapter.array); 
				//Set<String> set = new HashSet<String>();
				StringBuffer sBuffer = new StringBuffer();
				for (int j = 0; j < DeviceSceneAdapter.array.length; j++) {
					//set.add(DeviceSceneAdapter.array[j]+"");
					sBuffer.append(DeviceSceneAdapter.array[j]+",");
				}
				
				editor.remove(mSelectedGroup.getId()+"");
				editor.commit(); 
				editor.putString(mSelectedGroup.getId()+"", sBuffer.toString());
				//提交当前数据 
				editor.commit(); 
				Toast.makeText(EspSceneActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
			}
		});

        mDeviceLV = (ListView) findViewById(R.id.device_list);
        mDeviceList = new ArrayList<IEspDevice>();
        mDeviceList.addAll(mUserDeviceList);
        
      
        
        mDeviceAdapter = new DeviceAdapter(this, mAllDeviceList/*mDeviceList*/);
        mDeviceLV.setAdapter(mDeviceAdapter);
        mDeviceLV.setOnItemClickListener(this);
        mDeviceLV.setOnItemLongClickListener(this);
        mDeviceAdapter.setOnEditCheckedChangeListener(this);

        mSceneContent = findViewById(R.id.scene_content);
        mSceneAddIV = (ImageView) findViewById(R.id.scene_add_btn);
        mSceneAddIV.setOnClickListener(this);
        mSceneLV = (ListView) findViewById(R.id.scene_list);
        mUser.loadGroupDB();
        mSceneList = mUser.getGroupList();
        mSceneAdapter = new SceneAdapter(this);
        mSceneLV.setAdapter(mSceneAdapter);
        mSceneLV.setOnItemClickListener(this);
        mSceneLV.setOnItemLongClickListener(this);

        mEspGroupHandler = EspGroupHandler.getInstance();
        mEspGroupHandler.call();
        mEspGroupDBManager = EspGroupDBManager.getInstance();

        mBroadcastManager = LocalBroadcastManager.getInstance(this);
        IntentFilter filter = new IntentFilter(EspStrings.Action.CREATE_NEW_CLOUD_GROUP);
        mBroadcastManager.registerReceiver(mReceiver, filter);
    }
    protected List<IEspDevice> mAllDeviceList;
    private synchronized void updateDeviceList() {
        mAllDeviceList.clear();
        boolean hasMeshDevice = false;
        List<IEspDevice> list = mUser.getAllDeviceList();
        for (int i = 0; i < list.size(); i++) {
            IEspDevice device = list.get(i);
            IEspDeviceState state = device.getDeviceState();
            if (!state.isStateDeleted()) {
//                if (device.getIsMeshDevice()) {
//                    hasMeshDevice = true;
//                }
//                if (!mDeviceVisibleCB.isChecked()) {
//                    if (!state.isStateLocal() && !state.isStateInternet()) {
//                        continue;
//                    }
//                }
                if(!mAllDeviceList.contains(device)){
                    mAllDeviceList.add(device);
                }
            }
        }

//        new DeviceSortor().sort(mAllDeviceList, mSortType);
//
//        boolean showMeshTree = mShared.getBoolean(
//                EspStrings.Key.SETTINGS_KEY_SHOW_MESH_TREE, false);
//        if (hasMeshDevice && showMeshTree) {
//            mVirtuaMeshRoot.setName("Mesh Root");
//            mAllDeviceList.add(0, mVirtuaMeshRoot);
//        }
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();

        mBroadcastManager.unregisterReceiver(mReceiver);
    }

    private void updateGroupList() {
        mUser.loadGroupDB();
        mSceneList.clear();
        mSceneList.addAll(mUser.getGroupList());
        mSceneAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onTitleRightIconClick(View rightIcon) {
        PopupMenu popupMenu = new PopupMenu(this, rightIcon);
        Menu menu = popupMenu.getMenu();
        if (mSelectedGroup == null) {
            boolean canSyncLocal = mUser.isLogin() && mEspGroupDBManager.getUserGroup(null).size() > 0;
            menu.add(Menu.NONE, POPMENU_ID_SYNC_LOCAL, 0, R.string.esp_scene_menu_sync_local).setEnabled(canSyncLocal);
        } else {
            mEditMenuItem = menu.add(Menu.NONE, POPMENU_ID_EDIT, 0, R.string.esp_scene_menu_edit);
            mRemoveMenuItem = menu.add(Menu.NONE, POPMENU_ID_REMOVE, 0, R.string.esp_scene_menu_remove);
            mControlMenuItem = menu.add(Menu.NONE, POPMENU_ID_CONTROL, 0, R.string.esp_scene_menu_control);
            /************************* 手动添加的场景控制菜单项 *************************************/
            mSceneControlMenuItem = menu.add(Menu.NONE, POPMENU_ID_SCENE_CONTROL, 0,  R.string.esp_scene_menu_control_scene);
        }
        popupMenu.setOnMenuItemClickListener(this);
        updateMenuItems();
        popupMenu.show();
    }

    @Override
    public void onClick(View v) {
        if (v == mSceneAddIV) {
            showCreateSceneDialog();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mDeviceAdapter = new DeviceAdapter(this,mAllDeviceList /*mDeviceList*/);
        mDeviceLV.setAdapter(mDeviceAdapter);
       
        scene_excute.setVisibility(View.GONE);
        scene_save.setVisibility(View.GONE);
        if (parent == mSceneLV) {
            mDeviceAdapter.notifyDataSetChanged();
            mDeviceAdapter.getmEditCheckedDevices().clear();
            mDeviceAdapter.setEditable(false);
            updateMenuItems();

            IEspGroup group = mSceneList.get(position);
            if (mSelectedGroup == group) {
                mSelectedGroup = null;
                mDeviceList.clear();
                //updateDeviceList();
             
               mDeviceList.addAll(mAllDeviceList/*mUserDeviceList*/);
                setTitle(mTitle);
            } else {
                mSelectedGroup = group;
                mDeviceList.clear();
                mDeviceList.addAll(mSelectedGroup.getDeviceList());
                setTitle(mSelectedGroup.getName());
            }

            mDeviceAdapter.notifyDataSetChanged();
            mSceneAdapter.notifyDataSetChanged();
        } else if (parent == mDeviceLV) {
            IEspDevice device = mDeviceList.get(position);
            Intent intent = DeviceActivityAbs.getDeviceIntent(this, device);
            if (intent != null) {
                intent.putExtra(EspStrings.Key.DEVICE_KEY_KEY, device.getDeviceNumber());
                startActivity(intent);
            }
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        if (parent == mDeviceLV) {
            float offsetX = mSceneContent.getX();
            float offsetY = mSceneContent.getY() + mSceneAddIV.getHeight();
            int count = mSceneLV.getChildCount();
            List<IntersectsView> views = new ArrayList<IntersectsView>();
            for (int i = 0; i < count; i++) {
                View child = mSceneLV.getChildAt(i);
                float x = child.getX() + offsetX;
                float y = child.getY() + offsetY;
                RectF rectf = new RectF(x, y, x + child.getWidth(), y + child.getHeight());
                SceneIntersectsView siv = new SceneIntersectsView(child, rectf);
                views.add(siv);
            }

            View moveView = generateBitmapView(view);
            moveView.setTag(view.getTag());
            mMoveLayout.startTouchMove(moveView, view.getX(), view.getY(), views);

            return true;
        } else if (parent == mSceneLV) {
            IEspGroup group = mSceneList.get(position);
            PopupMenu popMenu = new PopupMenu(this, view);
            Menu menu = popMenu.getMenu();
            menu.add(Menu.NONE, POPMENU_ID_RENAME, 0, R.string.esp_scene_popmenu_rename);
            menu.add(Menu.NONE, POPMENU_ID_DELETE, 0, R.string.esp_scene_popmenu_delete);
            popMenu.setOnMenuItemClickListener(new SceneMenuListener(group));
            popMenu.show();

            return true;
        }

        return false;
    }

    /*************************************** 手动添加场景菜单功能 *************************************************************/
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
        case POPMENU_ID_SYNC_LOCAL:
            mEspGroupDBManager.updateLocalGroupUserKey(mUser.getUserKey());
            updateGroupList();
            mEspGroupHandler.call();
            return true;
        case POPMENU_ID_EDIT:
            boolean editable = !mDeviceAdapter.isEditable();
            mDeviceAdapter.setEditable(editable);
            if (!editable) {
                mDeviceAdapter.getmEditCheckedDevices().clear();
            }
            updateMenuItems();
            mDeviceAdapter.notifyDataSetChanged();
            return true;
        case POPMENU_ID_REMOVE:
            showRemoveDeviceDialog();
            return true;
        case POPMENU_ID_CONTROL:
            checkControllableDevice();
            return true;

            /*************************************** 手动添加场景菜单功能 *************************************************************/
        case POPMENU_ID_SCENE_CONTROL:
            transformSceneSetting();
            return true;
        }
        return false;
    }

    @Override
    public void onEditCheckedChanged(CheckBox checkBox, IEspDevice device, boolean isChecked) {
        updateMenuItems();
    }

    private void updateMenuItems() {
        if (mEditMenuItem != null) {
            mEditMenuItem.setEnabled(mSelectedGroup != null);
            int titleRes = mDeviceAdapter.isEditable() ? R.string.esp_scene_menu_edit_cancel : R.string.esp_scene_menu_edit;
            mEditMenuItem.setTitle(titleRes);
        }

        boolean hasDeviceSelected = mDeviceAdapter.getmEditCheckedDevices().size() > 0;
        if (mRemoveMenuItem != null) {
            mRemoveMenuItem.setEnabled(hasDeviceSelected);
        }
        if (mControlMenuItem != null) {
            mControlMenuItem.setEnabled(hasDeviceSelected);
        }

        /************************* 手动添加的场景控制菜单项 *************************************/
        if (mSceneControlMenuItem != null) {
            mControlMenuItem.setEnabled(true);
        }

    }

    private Bitmap generateViewBitmap(View view) {
        Bitmap bmp = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);
        view.draw(canvas);

        return bmp;
    }

    private View generateBitmapView(View view) {
        ImageView iv = new ImageView(this);
        LayoutParams lp = new LayoutParams(view.getWidth(), view.getHeight());
        iv.setLayoutParams(lp);
        iv.setScaleType(ScaleType.CENTER_INSIDE);
        iv.setImageBitmap(generateViewBitmap(view));
        iv.setBackgroundColor(getResources().getColor(R.color.esp_scene_move_view_background));

        return iv;
    }

    private OnTouchMoveListener mIntersectsListener = new OnTouchMoveListener() {

        @Override
        public void onIntersectsChanged(View moveView, View intersectsView) {
            IEspGroup group = intersectsView == null ? null : (IEspGroup) intersectsView.getTag();
            if (group != null) {
                System.out.println("group id = " + group.getId());
            }
            mSceneAdapter.setDeviceSelectedGroup(group);
            mSceneAdapter.notifyDataSetChanged();
        }

        @Override
        public void onTouchMoveEnd(View moveView, View intersectsView) {
            if (intersectsView != null) {
                IEspDevice device = (IEspDevice) moveView.getTag();
                IEspGroup group = (IEspGroup) intersectsView.getTag();
                mUser.doActionGroupDeviceMoveInto(device, group);
                mSceneAdapter.setDeviceSelectedGroup(null);
                updateGroupList();
                Toast.makeText(getBaseContext(), R.string.esp_scene_move_device_into_toast, Toast.LENGTH_SHORT).show();
            }
        }

    };

    private class SceneIntersectsView implements IntersectsView {
        private View mView;

        private RectF mRectF;

        public SceneIntersectsView(View view, RectF rectF) {
            mView = view;
            mRectF = rectF;
        }

        @Override
        public View getView() {
            return mView;
        }

        @Override
        public RectF getRectF() {
            return mRectF;
        }

    }

    private class SceneAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        private IEspGroup mDeviceIntersectsGroup;

        private final int COLOR_NORMAL;

        private final int COLOR_INTERSECTS;

        private final int COLOR_SELECTED;

        public SceneAdapter(Context context) {
            COLOR_NORMAL = Color.TRANSPARENT;
            COLOR_INTERSECTS = getResources().getColor(R.color.esp_scene_background_intersects);
            COLOR_SELECTED = getResources().getColor(R.color.esp_scene_background_selected);
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return mSceneList.size();
        }

        @Override
        public IEspGroup getItem(int position) {
            return mSceneList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return mSceneList.get(position).getId();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.esp_scene_list_item, null);
            }

            IEspGroup group = getItem(position);
            convertView.setTag(group);
            if (group == mDeviceIntersectsGroup) {
                convertView.setBackgroundColor(COLOR_INTERSECTS);
            } else if (group == mSelectedGroup) {
                convertView.setBackgroundColor(COLOR_SELECTED);
            } else {
                convertView.setBackgroundColor(COLOR_NORMAL);
            }

            TextView title = (TextView) convertView.findViewById(R.id.text);
            title.setText(group.getName());

            return convertView;
        }

        public void setDeviceSelectedGroup(IEspGroup group) {
            mDeviceIntersectsGroup = group;
        }
    }

    private class SceneMenuListener implements OnMenuItemClickListener {
        private IEspGroup mGroup;

        public SceneMenuListener(IEspGroup group) {
            mGroup = group;
        }

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
            case POPMENU_ID_RENAME:
                showEditSceneDialog(mGroup);
                return true;
            case POPMENU_ID_DELETE:
                showDeleteSceneDialog(mGroup);
                return true;
            }
            return false;
        }

    }

    private void showCreateSceneDialog() {
        showEditSceneDialog(null);
    }

    private void showEditSceneDialog(final IEspGroup scene) {
        View view = getLayoutInflater().inflate(R.layout.edit_dialog, null);
        final EditText edittext = (EditText) view.findViewById(R.id.edit);
        edittext.setHint(R.string.esp_scene_edit_hint);
        final TextView textview = (TextView) view.findViewById(R.id.text);
        textview.setVisibility(View.GONE);
        textview.setText(R.string.esp_scene_duplicate_scene_msg);
        final AlertDialog dialog = new AlertDialog.Builder(this).setView(view).setTitle(scene == null ? getString(R.string.esp_scene_create) : scene.getName())
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String groupName = edittext.getText().toString();
                        editGroup(scene, groupName);
                    }
                }).show();
        dialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);
        edittext.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                boolean duplicateName = false;
                for (IEspGroup group : mSceneList) {
                    if (s.toString().equals(group.getName())) {
                        duplicateName = true;
                        break;
                    }
                }
                textview.setVisibility(duplicateName ? View.VISIBLE : View.GONE);

                Button pButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
                boolean btnEnable = (!TextUtils.isEmpty(s)) && (!duplicateName);
                pButton.setEnabled(btnEnable);
            }
        });
    }

    private void editGroup(final IEspGroup scene, String groupName) {
        if (scene == null) {
            mUser.doActionGroupCreate(groupName);
        } else {
            mUser.doActionGroupRename(scene, groupName);
        }

        updateGroupList();
    }

    private void showDeleteSceneDialog(final IEspGroup group) {
        new AlertDialog.Builder(this).setTitle(R.string.esp_scene_delete_dialog_title).setMessage(getString(R.string.esp_scene_delete_dialog_msg, group.getName()))
                .setNegativeButton(android.R.string.cancel, null).setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mUser.doActionGroupDelete(group);

                        updateGroupList();
                        
                        
                        /******************************************/
                      //实例化SharedPreferences对象（第一步） 
        				SharedPreferences mySharedPreferences= getSharedPreferences("scene",Activity.MODE_PRIVATE); 
        				//实例化SharedPreferences.Editor对象（第二步） 
        				SharedPreferences.Editor editor = mySharedPreferences.edit(); 
        			    				
        				editor.remove(group.getId()+"");
        				//提交当前数据 
        				editor.commit(); 
        				
                        
                    }
                }).show();
    }

    private void showRemoveDeviceDialog() {
        new AlertDialog.Builder(this).setMessage(R.string.esp_scene_remove_dialog_msg).setNegativeButton(android.R.string.cancel, null)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        for (IEspDevice device : mDeviceAdapter.getmEditCheckedDevices()) {
                            mUser.doActionGroupDeviceRemove(device, mSelectedGroup);
                            mSelectedGroup.removeDevice(device);
                            mDeviceList.remove(device);
                        }
                        mDeviceAdapter.notifyDataSetChanged();
                    }
                }).show();
    }

    /************************* 手动添加的场景控制菜单项 *************************************/
    private void transformSceneSetting() {
        DeviceSceneAdapter mSceneDeviceAdapter = new DeviceSceneAdapter(this, mDeviceList);
        mDeviceLV.setAdapter(mSceneDeviceAdapter);
        mSceneDeviceAdapter.notifyDataSetChanged();

        scene_excute.setVisibility(View.VISIBLE);
        scene_save.setVisibility(View.VISIBLE);
    }

    private void checkControllableDevice() {
        Set<EspDeviceType> deviceTypes = new HashSet<EspDeviceType>();
        for (IEspDevice device : mDeviceAdapter.getmEditCheckedDevices()) {
            EspDeviceType type = device.getDeviceType();
            if (type == EspDeviceType.LIGHT || type == EspDeviceType.PLUG) {
                deviceTypes.add(type);
            }
        }
        if (deviceTypes.size() == 0) {
            Toast.makeText(this, R.string.esp_scene_no_controllable_device_msg, Toast.LENGTH_SHORT).show();
        } else if (deviceTypes.size() == 1) {
            EspDeviceType deviceType = deviceTypes.iterator().next();
            boolean allSameType = true;
            for (IEspDevice device : mDeviceAdapter.getmEditCheckedDevices()) {
                if (device.getDeviceType() != deviceType) {
                    allSameType = false;
                    break;
                }
            }
            if (allSameType) {
                gotoSelectedDeviceArray(deviceType);
            } else {
                showDeviceArraySelectDialog(deviceTypes);
            }
        } else {
            showDeviceArraySelectDialog(deviceTypes);
        }
    }

    private void showDeviceArraySelectDialog(Set<EspDeviceType> deviceTypes) {
        final String[] chars = new String[deviceTypes.size()];
        int i = 0;
        for (EspDeviceType type : deviceTypes) {
            chars[i] = type.toString();
            i++;
        }
        new AlertDialog.Builder(this).setItems(chars, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                EspDeviceType type = EspDeviceType.getEspTypeEnumByString(chars[which]);
                gotoSelectedDeviceArray(type);
            }
        }).show();
    }

    private void gotoSelectedDeviceArray(EspDeviceType type) {
        IEspDeviceArray deviceArray = BEspDevice.createDeviceArray(type);
        for (IEspDevice device : mDeviceAdapter.getmEditCheckedDevices()) {
            if (device.getDeviceType() == type) {
                deviceArray.addDevice(device);
            }
        }

        Intent intent = DeviceActivityAbs.getDeviceIntent(getBaseContext(), deviceArray);
        intent.putExtra(EspStrings.Key.DEVICE_KEY_SHOW_CHILDREN, false);
        intent.putExtra(EspStrings.Key.DEVICE_KEY_TEMP_DEVICE, true);
        DeviceActivityAbs.TEMP_DEVICE = deviceArray;
        startActivity(intent);
    }

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (action.equals(EspStrings.Action.CREATE_NEW_CLOUD_GROUP)) {
                long oldId = intent.getLongExtra(EspStrings.Key.KEY_GROUP_ID_OLD, 0);
                long newId = intent.getLongExtra(EspStrings.Key.KEY_GROUP_ID_NEW, 0);
                updateGroupId(oldId, newId);
            }
        }

    };

    private void updateGroupId(long oldId, long newId) {
        for (IEspGroup group : mSceneList) {
            if (group.getId() == oldId) {
                group.setId(newId);
                return;
            }
        }
    }
}

